import pygame
import sys
import os
from utils.config import SCREEN_WIDTH, SCREEN_HEIGHT, BG_IMAGE, FONT_NAME

class DescriptionInterface:
    def __init__(self, bg_image_filename=None, position=None, size=None):
        pygame.init()
        if size:
            self.width, self.height = size
        else:
            self.width, self.height = SCREEN_WIDTH, SCREEN_HEIGHT
        if position:
            os.environ['SDL_VIDEO_WINDOW_POS'] = f"{position[0]},{position[1]}"
        self.screen = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption("Snake Game - Description")
        self.font = pygame.font.SysFont(FONT_NAME, 48)
        self.small_font = pygame.font.SysFont(FONT_NAME, 32)
        self.bg_image = None
        image_file = bg_image_filename if bg_image_filename else BG_IMAGE
        if image_file:
            assets_path = os.path.join(os.path.dirname(__file__), '..', 'assets')
            image_path = os.path.join(assets_path, image_file)
            if os.path.exists(image_path):
                self.bg_image = pygame.image.load(image_path)
                self.bg_image = pygame.transform.scale(self.bg_image, (self.width, self.height))

    def show(self):
        running = True
        description_lines = [
            "Snake game.",
            "Press arrow keys to move.",
            "Eat apples to grow.",
            "Avoid walls and your own tail."
        ]
        while running:
            if self.bg_image:
                self.screen.blit(self.bg_image, (0, 0))
            else:
                self.screen.fill((0, 0, 0))

            for i, line in enumerate(description_lines):
                text_surface = self.font.render(line, True, (255, 255, 255))
                text_rect = text_surface.get_rect(center=(self.width // 2, 200 + i * 80))
                self.screen.blit(text_surface, text_rect)

            tip_surface = self.small_font.render("Press any key to return", True, (255, 255, 0))
            tip_rect = tip_surface.get_rect(center=(self.width // 2, self.height - 100))
            self.screen.blit(tip_surface, tip_rect)

            pygame.display.flip()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.KEYDOWN or (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
                    pygame.time.delay(200)
                    return